//
//  LLMallCatalogCollectionViewCell.m
//  lrlzapp
//
//  Created by dl on 16/3/1.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLMallCatalogCollectionViewCell.h"
#import "LLMenuItemDataCollectionViewModel.h"

#define CONSTANT_ITEM_WIDTH         SUIT_W_320(50)
#define CONSTANT_ITEM_HEIGHT        self.ll_height-SUIT_H(8)-SUIT_LINE_WIDTH
#define CONSTANT_ITEM_SPACE         round((FULL_WIDTH-4*CONSTANT_ITEM_WIDTH)/4.f)

@interface CategoryItemView : UIView
@property(nonatomic, strong) LLUIImageView *imgView;
@property(nonatomic, strong) UILabel *bottomLabel;
@end

@interface LLMallCatalogCollectionViewCell ()
@property(nonatomic, strong) LLMenuItemDataCollectionViewModel *viewModel;
@property(nonatomic, strong) UIScrollView *scrollView;
@end

@implementation LLMallCatalogCollectionViewCell
@synthesize scrollView;

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // 最上面的线
        CALayer *layer = [CALayer layer];
        layer.frame = CGRectMake(0, 0, self.ll_width, SUIT_LINE_WIDTH);
        layer.backgroundColor = UI_COLOR_LINE_GREY.CGColor;
        [self.layer addSublayer:layer];

        scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0,
                SUIT_LINE_WIDTH,
                self.ll_width,
                CONSTANT_ITEM_HEIGHT)];
        scrollView.backgroundColor = [UIColor whiteColor];
        scrollView.showsHorizontalScrollIndicator = NO;
        scrollView.alwaysBounceHorizontal = YES;
        [self.contentView addSubview:scrollView];
    }
    return self;
}

- (void)bindViewModel:(LLMenuItemDataCollectionViewModel *)viewModel {
    self.viewModel = viewModel;
    // 图片列表
    NSArray<LLMenuItemDataViewModel *> *items = viewModel.itemList;
    for (int i = 0; i < items.count; i++) {
        CGRect frame = CGRectMake(
                CONSTANT_ITEM_SPACE / 2.f + i * (CONSTANT_ITEM_WIDTH + CONSTANT_ITEM_SPACE),
                0,
                CONSTANT_ITEM_WIDTH,
                CONSTANT_ITEM_HEIGHT
        );
        CategoryItemView *itemView = [[CategoryItemView alloc] initWithFrame:frame];
        [itemView.imgView setImageUrlStr:items[i].imageUrl];
        itemView.bottomLabel.text = items[i].title;
        [scrollView addSubview:itemView];
    }
    CGFloat scrollWidth = scrollView.ll_width;
    CGFloat sumWidth = (CONSTANT_ITEM_WIDTH + CONSTANT_ITEM_SPACE) * items.count;
    if (sumWidth > scrollWidth) {
        scrollView.contentSize = CGSizeMake(sumWidth, scrollView.ll_height);
    }
}

@end

@implementation CategoryItemView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        _imgView = [[LLUIImageView alloc] initWithFrame:CGRectMake(
                0,
                SUIT_H_320(12),
                SUIT_W_320(50),
                SUIT_H_320(74)
        )];
        [self addSubview:_imgView];

        _bottomLabel = [[UILabel alloc] initWithFrame:CGRectMake(
                0,
                self.ll_height - SUIT_H_320(15),
                self.ll_width,
                SUIT_H_320(15)
        )];
        _bottomLabel.textAlignment = NSTextAlignmentCenter;
        _bottomLabel.textColor = UI_COLOR_FONT_GREY;
        _bottomLabel.font = SUIT_FONT_320(12);
        [self addSubview:_bottomLabel];
    }
    return self;
}

@end